﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Cashier_Utils;

namespace Cashier.utils
{
    public class GenearteDataGridViewColumn
    {
        /// <summary>
        /// 返回一个DataGridView数据列
        /// </summary>
        /// <param name="dataFieldName">对应数据库表的字段名称</param>
        /// <param name="headerText">头文字</param>
        /// <param name="isReadOnly">是否只读数据列</param>
        /// <param name="fillWeight">列宽，以百分比计 如90%，即9</param>
        /// <param name="itemAlign">内容对齐方式</param>
        /// <param name="headerAlign">头文字对齐方式</param>
        /// <param name="isDisplay">是否显示列</param>
        /// <param name="isDisplay">是否允许列排序</param>
        /// <returns></returns>
        public static DataGridViewTextBoxColumn GetDataGridViewDataFieldColumn(string dataFieldName, string headerText, bool isReadOnly, int fillWeight,
            string itemAlign, string headerAlign, bool isDisplay, bool isSort)
        {
            DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn();
            textColumn.Name = dataFieldName;
            textColumn.DataPropertyName = dataFieldName;
            if (isDisplay)
            {
                textColumn.HeaderText = headerText;
                textColumn.ReadOnly = isReadOnly;
                textColumn.FillWeight = fillWeight;
                if (itemAlign.Equals(ActKey.LEFT))
                    textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                else if (itemAlign.Equals(ActKey.CENTER))
                    textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                else
                    textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

                if (headerAlign.Equals(ActKey.LEFT))
                    textColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                else if (headerAlign.Equals(ActKey.CENTER))
                    textColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                else
                    textColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
            if (isSort)
                textColumn.SortMode = DataGridViewColumnSortMode.Automatic;
            else
                textColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            textColumn.Visible = isDisplay;
            return textColumn;
        }

        /// <summary>
        /// 返回一列空的DataGridView数据列
        /// </summary>
        /// <param name="headerText"></param>
        /// <param name="isReadOnly"></param>
        /// <param name="fillWeight"></param>
        /// <param name="itemAlign"></param>
        /// <param name="headerAlign"></param>
        /// <param name="isDisplay"></param>
        /// <returns></returns>
        public static DataGridViewTextBoxColumn GetEmptyDataGridViewColumn(string headerText, bool isReadOnly, int fillWeight, string itemAlign,
            string headerAlign, bool isDisplay)
        {
            DataGridViewTextBoxColumn textColumn = new DataGridViewTextBoxColumn(); 
            textColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            textColumn.HeaderText = headerText;
            textColumn.ReadOnly = isReadOnly;
            textColumn.FillWeight = fillWeight;
            if (itemAlign.Equals(ActKey.LEFT))
                textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            else if (itemAlign.Equals(ActKey.CENTER))
                textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            else
                textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;

            if (headerAlign.Equals(ActKey.LEFT))
                textColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            else if (headerAlign.Equals(ActKey.CENTER))
                textColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            else
                textColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomRight;
            textColumn.Visible = isDisplay;
            return textColumn;
        }

        /// <summary>
        /// 返回一列CheckBox列
        /// </summary>
        /// <param name="headerText"></param>
        /// <param name="isReadOnly"></param>
        /// <param name="fillWeight"></param>
        /// <param name="itemAlign"></param>
        /// <param name="headerAlign"></param>
        /// /// <param name="trueValue">复选框被选中时的值</param>
        /// <param name="isDisplay"></param>
        /// <returns></returns>
        public static DataGridViewCheckBoxColumn GetDataGridViewCheckBoxColumn(string headerText, bool isReadOnly, int fillWeight, string itemAlign,
            string headerAlign, bool isDisplay)
        {
            DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
            checkboxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            checkboxColumn.HeaderText = headerText;
            checkboxColumn.ReadOnly = isReadOnly;
            checkboxColumn.FillWeight = fillWeight;

            if (itemAlign.Equals(ActKey.LEFT))
                checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            else if (itemAlign.Equals(ActKey.CENTER))
                checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            else
                checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            if (headerAlign.Equals(ActKey.LEFT))
                checkboxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            else if (headerAlign.Equals(ActKey.CENTER))
                checkboxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            else
                checkboxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            checkboxColumn.Visible = isDisplay;
            return checkboxColumn;
        }

        /// <summary>
        /// 返回一个图像列
        /// </summary>
        /// <param name="DataField"></param>
        /// <param name="headerText"></param>
        /// <param name="heardImg"></param>
        /// <param name="alt"></param>
        /// <param name="fillWeight"></param>
        /// <param name="itemAlign"></param>
        /// <param name="headerAlign"></param>
        /// <returns></returns>
        public static DataGridViewImageColumn GetDataGridViewImageColumn(string dataField, string headerText, string heardImg, string alt, int fillWeight,
            string itemAlign, string headerAlign)
        {
            DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
            imageColumn.Name = dataField;
            imageColumn.DataPropertyName = dataField;
            imageColumn.HeaderText = headerText;
            imageColumn.ToolTipText = alt;
            imageColumn.Description = alt;
            imageColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
            //imageColumn.Image = new Bitmap(Application.StartupPath + @"\images\" + rowImg);
            imageColumn.FillWeight = fillWeight;
            imageColumn.SortMode = DataGridViewColumnSortMode.NotSortable;

            if (itemAlign.Equals(ActKey.LEFT))
                imageColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            else if (itemAlign.Equals(ActKey.CENTER))
                imageColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            else
                imageColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            if (headerAlign.Equals(ActKey.LEFT))
                imageColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            else if (headerAlign.Equals(ActKey.CENTER))
                imageColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            else
                imageColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;

            imageColumn.Visible = true;
            return imageColumn;
        }

        /// <summary>
        /// 返回一个DataGridView下选列表数据列
        /// </summary>
        /// <param name="dataFieldName">对应数据库表的字段名称</param>
        /// <param name="headerText">头文字</param>
        /// <param name="isReadOnly">是否只读数据列</param>
        /// <param name="fillWeight">列宽，以百分比计 如90%，即9</param>
        /// <param name="itemAlign">内容对齐方式</param>
        /// <param name="headerAlign">头文字对齐方式</param>
        /// <param name="isDisplay">是否显示列</param>
        /// <param name="isDisplay">是否允许列排序</param>
        /// <returns></returns>
        public static DataGridViewComboBoxColumn GetDataGridViewComboBoxColumn(string dataFieldName, string headerText, bool isReadOnly, int fillWeight,
            string itemAlign, string headerAlign, bool isDisplay, bool isSort)
        {
            DataGridViewComboBoxColumn comboBoxColumn = new DataGridViewComboBoxColumn();
            comboBoxColumn.Name = dataFieldName;
            comboBoxColumn.DataPropertyName = dataFieldName;
            if (isDisplay)
            {
                comboBoxColumn.HeaderText = headerText;
                comboBoxColumn.ReadOnly = isReadOnly;
                comboBoxColumn.FillWeight = fillWeight;
                if (itemAlign.Equals(ActKey.LEFT))
                    comboBoxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                else if (itemAlign.Equals(ActKey.CENTER))
                    comboBoxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                else
                    comboBoxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

                if (headerAlign.Equals(ActKey.LEFT))
                    comboBoxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                else if (headerAlign.Equals(ActKey.CENTER))
                    comboBoxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                else
                    comboBoxColumn.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
            if (isSort)
                comboBoxColumn.SortMode = DataGridViewColumnSortMode.Automatic;
            else
                comboBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            comboBoxColumn.Visible = isDisplay;
            return comboBoxColumn;
        }
    }
}
